package org.spring.springboot.controller.join;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.joda.time.DateTime;
import org.spring.springboot.controller.BaseController;
import org.spring.springboot.domain.join.TsFastJoinInfo;
import org.spring.springboot.service.join.TsFastJoinInfoService;
import org.spring.springboot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;

/**
 * @author zhaobo
 * @version 1.0
 * @date 2019/6/15 18:19
 */
@Api(value = "加盟Api", description = "加盟Api")
@RestController
@RequestMapping(value = "/shop-join-info")
public class TsFastJoinInfoController extends BaseController {

    @Autowired
    private TsFastJoinInfoService tsFastJoinInfoService;

    @ApiOperation(value = "修改审批", notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult update(
            @ApiParam(name = "joinPhone", value = "(必传)", required = true) @RequestParam String joinPhone,
            @ApiParam(name = "joinCreateTime", value = "(加盟者的留言创建时间yyyyMMdd 必传)", required = true)@RequestParam String joinCreateTime,
            @ApiParam(name = "joinAuditRemark", value = "(审批备注)", required = false)@RequestParam String joinAuditRemark,
            @ApiParam(name = "joinAuditFlag", value = "(0:正在审批，1:审批通过，-1:审批未通过)", required =true)@RequestParam String joinAuditFlag) {

        Timestamp joinAuditTime  = new Timestamp(System.currentTimeMillis());
        return success(tsFastJoinInfoService.updateAuditByKey(joinPhone, joinCreateTime, joinAuditRemark, joinAuditTime, joinAuditFlag));
    }

    @ApiOperation(value = "新增加盟信息", notes = "")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insert(
            @ApiParam(name = "joinPhone", value = "(必传)", required = true) @RequestParam String joinPhone,
            @ApiParam(name = "joinName", value = "(必传)", required = true) @RequestParam String joinName,
            @ApiParam(name = "joinAuditRemark", value = "(审批备注)", required = false)@RequestParam String joinAuditRemark ) {

        TsFastJoinInfo info = new TsFastJoinInfo();
        DateTime dateTime = new DateTime(System.currentTimeMillis());
        String time = dateTime.toString(super.DATE_DAY);
        info.setJoinCreateTime(time);
        info.setJoinName(joinName);
        info.setJoinPhone(joinPhone);
        info.setJoinRemark(joinAuditRemark);
        TsFastJoinInfo tsFastJoinInfo = tsFastJoinInfoService.queryByKey(info);
        if(tsFastJoinInfo != null){
            return success("当天只能申请一次加盟");
        }

        return success(tsFastJoinInfoService.insert(info));
    }

    @ApiOperation(value = "加盟信息查询", notes = "")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult query(String offset,String limit, String startTime,String endTime,String joinName,String joinPhone) {
        return success(tsFastJoinInfoService.selectForPage(offset,limit,startTime,endTime,joinName,joinPhone));
    }

    @ApiOperation(value = "加盟信息删除", notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult delete(
            @ApiParam(name = "joinPhone", value = "(必传)", required = true) @RequestParam String joinPhone,
            @ApiParam(name = "joinCreateTime", value = "(加盟者的留言创建时间yyyyMMdd 必传)", required = true)@RequestParam String joinCreateTime) {
        TsFastJoinInfo info = new TsFastJoinInfo();
        info.setJoinPhone(joinPhone);
        info.setJoinCreateTime(joinCreateTime);
        return success(tsFastJoinInfoService.delete(info));
    }


}
